<template id="c-cate">
    <div>
        <div class="title-bar">{{ cateForm.title }}</div>
        <el-form ref="form" :model="cateForm" label-width="80px" style="margin-top: 10px" label-position="left">
            <el-form-item label="文字颜色">
                <el-color-picker v-model="cateForm.fontColor" style="float: right" size="small"></el-color-picker>
            </el-form-item>
            <el-form-item label="背景颜色">
                <div style="float: right;text-align: center">
                    <el-color-picker v-model="cateForm.bgColor1" size="small" style="margin-right: 10px"></el-color-picker>
                    <el-color-picker v-model="cateForm.bgColor2" size="small"></el-color-picker>
                </div>
            </el-form-item>
            <el-form-item label="页面间距">
                <el-slider
                        @change="marginChange"
                        v-model="cateForm.pageMargin"
                        input-size="mini"
                        show-input>
                </el-slider>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    Vue.component('cate', {
        template: '#c-cate',
        props: {
            comData: {
                type: Object
            }
        },
        data() {
            return {
                cateForm: {
                    type: 'cate',
                    title: '商品分类', // 标题
                    fontColor: '#fff',
                    bgColor1: '#f62c2c',
                    bgColor2: '#f96e29',
                    pageMargin: 0 // 页面间距
                },
                cate: []
            }
        },
        computed: {
            componentData() {
                return JSON.parse(JSON.stringify(this.cateForm));
            },
        },
        watch: {
            componentData: {
                handler(newVal, oldVal) {
                    if (!oldVal) {
                        return false;
                    }

                    this.$emit('diy', {type: 'cate', form: this.componentData, data: this.cate})
                },
                deep: true
            },
            comData: {
                handler(newVal) {
                    this.cateForm = newVal
                },
                deep: true
            }
        },
        created() {
            if (this.comData) {
                this.cateForm = this.comData
            }
        },
        mounted() {
            this.getGoodsCate()
        },
        methods: {
            // 间距改变
            marginChange(val) {
                this.cateForm.pageMargin = val
            },
            // 获取商品分类
            async getGoodsCate() {
                let res = await request.get("/addons/diy/admin.api/getGoodsCate")
                this.cate = res.data

                this.$emit('diy', {type: 'cate', form: this.componentData, data: this.cate})
            }
        }
    })
</script>

<style>

</style>